package accounting_system;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;

public final class DatePanel extends JComponent{
    private int day, mounth, year;
    
    //private Calendar c = new GregorianCalendar();
    private JFormattedTextField dayField = new JFormattedTextField();
    private JFormattedTextField mounthField = new JFormattedTextField();
    private JFormattedTextField yearField = new JFormattedTextField();
    //private String[] mounthName = {"января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"};
    
    private boolean edit;  // = false;
    
//    public DatePanel(int day, int mounth, int year){
//        this.day = day;
//        this.mounth = mounth;
//        this.year = year;
//        init();
//    }
//    
//    public DatePanel(){
////        day = GregorianCalendar.getInstance().get(GregorianCalendar.DAY_OF_MONTH);
////        mounth = GregorianCalendar.getInstance().get(GregorianCalendar.MONTH);
////        year = GregorianCalendar.getInstance().get(GregorianCalendar.YEAR);
//        init();
//    }
    
    public DatePanel(String d){
        //System.out.println(d);
        String date = d;
        if (d != null){
            day = Integer.parseInt(date.substring(0, 2));
            mounth = Integer.parseInt(date.substring(2, 4));
            year = Integer.parseInt(date.substring(4));
            //System.out.println(day + " " + mounth + " " + year);
            setDay(day);
            setMounth(mounth);
            setYear(year);
        }
        init();
    }
    
    public void setDay(int d){
        
        if (d < 1 || d > 31) day = GregorianCalendar.getInstance().get(GregorianCalendar.DAY_OF_MONTH); 
        else day = d;
        dayField.setText(transform(day));
        //System.out.println("\"" + transform(day) + "\"");
    }
    
    public int getDay(){
        return day;
    }
    
    public void setMounth(int m){
        if (m < 1 || m > 12) mounth = GregorianCalendar.getInstance().get(GregorianCalendar.MONTH);
        else mounth = m;
        mounthField.setText(transform(mounth));
    }
    
    public int getMounth(){
        return mounth;
    }
    
    public void setYear(int y){
        int currYear = GregorianCalendar.getInstance().get(GregorianCalendar.YEAR);
        if (y < 1900 || y > (currYear)) year = currYear;
        else year = y;
        yearField.setText("" + year);
    }
    
    public int getYear(){
        return year;
    }
    
    public void setEditable(boolean edit){
        this.edit = edit;
        dayField.setEditable(edit);
        mounthField.setEditable(edit);
        yearField.setEditable(edit);
    }
    
    public boolean isEditable(){
        return edit;
    }
    
    private String transform(int v){
        return (v < 10) ? "0" + v: "" + v;
    }
    
    private void init(){
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        
        add(dayField, gbc);
        add(mounthField, gbc);
        add(yearField, gbc);
        
        dayField.setColumns(2);
        mounthField.setColumns(2);
        yearField.setColumns(3);
        
//        MaskFormatter mf = null;
//        try {
//            mf = new MaskFormatter("##");
//        } catch (ParseException ex) {
//            Logger.getLogger(DatePanel.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        mf.setValidCharacters("0123456789");
//        mf.setPlaceholderCharacter('*');
//        dayField = new JFormattedTextField(mf);
        
//        NumberFormat nf = NumberFormat.getInstance();
//        nf.setMaximumIntegerDigits(2);
//        dayField = new JFormattedTextField(nf);
//        
//        mounthField = new JFormattedTextField(nf);
//        
//        nf.setMaximumIntegerDigits(4);
//        yearField = new JFormattedTextField(nf);
        
        setEditable(edit = false);
//        setDay(day);
//        setMounth(mounth);
//        setYear(year);
    }
    
    
}
